#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@fileName     : todo.py
@description  : 
@time         : 2022年01月28日 11:18
@author       : WeiTingjun
@versions     : 1.0
'''
import time
import datetime

from epwechat import WeCom
from todo_file import get_all_todos, cover_save_todos
from log import logger


class Todo(WeCom):
    def __init__(self):
        super(Todo, self).__init__()

    def send_message(self):
        '''
        读取本地文件，并发送消息
        '''
        what_day = time.strftime('%A', time.localtime())  # 星期几
        year_month_day = datetime.datetime.now().strftime('%Y%m%d')  # 具体到天，年月日，如：20201009
        day = int(datetime.datetime.now().strftime('%d'))  # 具体到天，每个月的第几日，如：9
        origin_list = get_all_todos()  # 所有待办
        for item in origin_list:
            message = item.split('|')
            period = message[0]  # 时间
            if period == 'Everyday' or period == what_day or period == year_month_day or period == str(day):
                today = datetime.datetime.today()
                now = today.strftime('%H:%M')
                if now == message[1]:  # 当前时间是否与配置文件的相等
                    logger.info('正要通知的待办：{}'.format(message))
                    self.message.__setitem__('title', message[2])
                    self.message.__setitem__('detail', message[3])
                    if len(message) > 4:
                        users_id = message[4].replace(',', '|')
                    else:
                        users_id = None
                    self.is_textcard_msg = True  # 消息格式
                    self.send_msg(to_users=users_id)

    def delete_past_due(self):
        '''
        删除已过期提醒事件，比如当前时间20210507，提醒事件时间20210505，则把这个提醒事件删除
        '''
        year_month_day = datetime.datetime.now().strftime('%Y%m%d')  # 具体到天，年月日，如：20201009
        new_list = []
        origin_list = get_all_todos()  # 获取待办库
        for item in origin_list:
            sub_list = item.split('|')
            period = sub_list[0]
            if len(period) == 8 and period < year_month_day:
                pass
            else:
                new_list.append(item)
        if len(new_list) != len(origin_list):  # 存在删除项，更新待办库
            cover_save_todos(new_list)  # 更新待办库
